home *** CD-ROM | disk | FTP | other *** search
- Path: news.compuserve.com!newsmaster
- From: Philippe Verdy <100105.3120@compuserve.com>
- Newsgroups: comp.lang.c++
- Subject: Re: 2 D arrays in C++ constructors
- Date: 1 Apr 1996 23:42:04 GMT
- Organization: CompuServe Incorporated
- Message-ID: <4jppkc$lhj@dub-news-svc-6.compuserve.com>
- NNTP-Posting-Host: ad15-078.compuserve.com
-
- rsuri@csugrad.cs.vt.edu (Raj Suri) s'Θcrit :
- > Ok,
- > I wrote a c++ class that used an array of integers. However, I didn't know the
- > size of the array until the constructor was called. So I did this:
- >
- > class ... {
- >
- > private:
- > int* ptr;
- >
- > And then in the constructor, I had this code:
- >
- > ptr = new int[10];
- >
- > This works fine. However, now I need to use a two dimensional array in
- > which I don't know the dimensions until the constructor is called. I can't
- > figure out how to declare ptr and allocate space to it such that I could do the
- > following statement:
- >
- > ptr[2][3] = 4;
- >
- > Any help.. Thanks in advance.
- > -Raj
- >
- If you don't know the first dimensions, there will be no way
- to declare a pointer for a 2D array.
- However, you can build up a class which internally allocates
- a 1D array and an appropriate constructor which allocates the
- product of the dimensions. Then associate is to your pointer
- type, by registrating this pointer with the actual dimensions
- used, and then add an array index operator[] to do the
- first indirection (it will return a 1D array pointer, i.e. a
- simple pointer, on which the standard 1D [] index operator can
- apply...)
-